<?php
require 'include.php';
$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
$inscription = (!empty($_REQUEST['inscription'])) ? $_REQUEST['inscription'] : "";
if($inscription){
	$user_id = check_cookie();
	$return = "";
	$activite = new activite($inscription);
	if($activite->valide){
		if($action=='delete'){
			if(!empty($_REQUEST['id_inscription'])){
				$q = "delete from activite_$activite->id where id = ".$_REQUEST['id_inscription'];
				ExecRequete($q);
			}
			$return = "suppression confirm&eacute;e";
		}
		elseif($action == 'update'){
			$activite->formulaire($_REQUEST['id_inscription']);
		}
		else{
			$membre = new membre($user_id);
			if($activite->prive && !$user_id) $return = "L'activité est réservée aux membres du VBCLL";
			else{
				$fields = describe('activite_'.$activite->id);
				foreach($fields as $name => $type){
					$$name = (!empty($_REQUEST[$name])) ? $_REQUEST[$name] : "";
				}
				if($activite->prive && $activite != 'update'){
					$nom = $membre->prenom." ".$membre->nom;
					$email = $membre->email;
				}
				if($user_id && $activite != 'update' && $activite->prive) $id = $user_id;
				$q = "replace into activite_$activite->id set ";
				$texte = "";
				foreach($fields as $name => $type){
					$q .= "`$name` = '".(addslashes($$name))."', ";
					if($name != 'id') $texte .= "$name : ".$$name."\n";
				}
				$q = substr($q,0,-2);
				$return = (ExecRequete($q)) ? "Inscription confirm&eacute;e" : "Désolé, une erreur est survenue...";
				if($return == "Inscription confirm&eacute;e"){
					$from = "vbcll@vbcll.ch";
					$subject = "VBCLL : inscription $activite->nom";
					$content  = $activite->texte_email;
					$content .= "\n\nVous avez fourni les données suivantes : \n\n $texte";
					$headers =    "MIME-Version: 1.0\r\n" . 
					               "Content-type: text/plain; charset=utf-8; format=flowed\r\n" . 
					               "Content-Transfer-Encoding: 8bit\r\n" . 
					               "From: $from\r\n" . 
					               "X-Mailer: PHP" . phpversion(); 
					if(strpos($email,"@") !== FALSE){
						mail($email,$subject,$content,$headers);	
					}
				}
			}			
		}
	}
	else $return = "Les inscriptions ne sont pas possibles";
	$edit = ($activite->responsable == $user_id || check_admin()) ? 1 : 0;
	if($action != 'update') echo $activite->inscriptions($edit)."<--->".$return;
}
else{
	$activite = new activite($id);
	if($activite->valide){
		$user_id = check_cookie();

		$edit = ($activite->responsable == $user_id || check_admin()) ? 1 : 0;
		$activite->resume();
		echo "<div id = 'inscrits'>";
		$activite->inscriptions($edit);
		echo "</div>\n";
		echo "<h3>Inscription<span class = 'float_right'>jusqu'au: ".(format_date($activite->delai))."</span></h3>\n";
		if($activite->delai >= date('Y-m-d')){
			if(($activite->prive && $user_id) || $activite->prive == 0 ){
				echo "<div id = 'inscription'>\n";
				$activite->formulaire();		
				echo "</div>\n";
			}
			else{
				echo "<p class = 'center'>Cette activit&eacute; est r&eacute;serv&eacute;e aux membres du VBCLL</p>\n";
				echo "<p class = 'center'>Si vous &ecirc;tes membre, identifiez-vous sur le site $user_id $activite->prive</p>\n";
			}
		}
		else{
			echo "<p class = 'center'>Le d&eacute;lai d'inscription est pass&eacute;. Il n'est plus possible de s'inscrire.</p>\n";
		}

	}
	
}
?>